package com.chixing.day05_oop.work;

import java.util.ArrayList;
import java.util.Scanner;

public class OrderSystem {
    public static void main(String[] args) {
        //假定这个系统可以存储1000个订单
        Order[] orders=new Order[1000];
        //订单数组长度
        int size=0;
        while (true) {
            System.out.println("请输入订单类型 0:个人订单 1:公司订单 2:退出系统");
            Scanner sc = new Scanner(System.in);
            int command = sc.nextInt();
            if (command == 1) {
                //现在有一个随机的订单需要存入 订单系统
                CompanyOrder companyOrder = new CompanyOrder();
                System.out.println("请输入订单id");
                int id = sc.nextInt();
                companyOrder.setOrderId(id);
                System.out.println("请输入公司名称");
                String name = sc.next();
                companyOrder.setCompanyName(name);
                System.out.println("请输入订单数量");
                int orderNumber = sc.nextInt();
                companyOrder.setOrderNumber(orderNumber);
                //判断拆分多少次 拆一次 number*2=2 拆两次number*2=4
                int number = 1;
                //判断这个输入的订单是不是有没有大于1000
                //如果大于1000则进行拆分，直到拆分到小于1000为止
                while (companyOrder.getOrderNumber() > 1000) {
                    //当前订单数量拆分一半
                    companyOrder.setOrderNumber(companyOrder.getOrderNumber() / 2);
                    number = number * 2;
                }

                while (number > 1) {
                    //备份一个执行次数
                    int temp = number;//4
                    number /= 2;//2
                    while (temp > number) {
                        orders[size++] = companyOrder;
                        temp--;
                    }
                }
                if (number == 1)
                    orders[size++] = companyOrder;
                //操作结束断开与内存关系
                companyOrder = null;
            } else if (command == 0) {
                //现在有一个随机的订单需要存入 订单系统
                PersonOrder personOrder = new PersonOrder();
                System.out.println("请输入订单id");
                int id = sc.nextInt();
                personOrder.setOrderId(id);
                System.out.println("请输入个人姓名");
                String name = sc.next();
                personOrder.setPersonName(name);
                System.out.println("请输入订单数量");
                int orderNumber = sc.nextInt();
                personOrder.setOrderNumber(orderNumber);
                //判断拆分多少次 拆一次 number*2=2 拆两次number*2=4
                int number = 1;
                //判断这个输入的订单是不是有没有大于1000
                //如果大于1000则进行拆分，直到拆分到小于1000为止
                while (personOrder.getOrderNumber() > 1000) {
                    //当前订单数量拆分一半
                    personOrder.setOrderNumber(personOrder.getOrderNumber() / 2);
                    number = number * 2;
                }

                while (number > 1) {
                    //备份一个执行次数
                    int temp = number;//4
                    number /= 2;//2
                    while (temp > number) {
                        orders[size++] = personOrder;
                        temp--;
                    }
                }
                if (number == 1)
                    orders[size++] = personOrder;
                //操作结束断开与内存关系
                personOrder = null;
            }else
                break;

            for (int i = 0; i < size; i++) {
                System.out.println(orders[i]);
            }
        }
    }
}
